<?php

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\SystemConfig;
use App\Lib\Bls;
use App\Http\Model\FinanceBank;
use App\Http\Model\UserVerify;
use App\Http\Model\FinanceUserWallet;
use App\Http\Model\FinanceWithdrawOrder;

class BankBlsServices extends CommonServices
{
    protected $financeBankModel;
    protected $blsLib;
    protected $userVerifyModel;
    private $model_finance_user_wallet;
    private $model_finance_withdraw_order;
    private $systemConfigModel;
    public function __construct()
    {
        $this->financeBankModel = new FinanceBank();
        $this->blsLib = new Bls();
        $this->userVerifyModel = new UserVerify();
        $this->model_finance_user_wallet = new FinanceUserWallet();
        $this->model_finance_withdraw_order = new FinanceWithdrawOrder();
        $this->systemConfigModel = new SystemConfig();
    }

    public function getBankUserBase($userId)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        //实名认证
        $user_verify = $this->userVerifyModel->getInfoByUser(['user_id'=>$userId ,'result_code'=>1000]);
        if(empty($user_verify)) {
            $code = Hint::USER_NOT_CERTIFICATION;
        }else{
            $bankInfo = $this->model_finance_withdraw_order->getUserAlipay($userId, 0);
            $data['name'] = $user_verify['idcard_name'];
            $data['cardNo'] = $user_verify['idcard_number'];
            $data['openName'] = $user_verify['idcard_name'];
            $data['worker_account'] = empty($bankInfo['worker_account']) ? '' : $bankInfo['worker_account']; //支付宝账号
            $data['bank_code'] = empty($bankInfo['bank_code']) ? '' : $bankInfo['bank_code'];//银行卡号
            $data['bank_source'] = empty($bankInfo['bank_source']) ? '' : $bankInfo['bank_source'];;
        }

        //可提现金额
        $wallet = $this->model_finance_user_wallet->get_wallet($userId);
        $data['kk_balance'] = empty($wallet['kk_balance']) ? 0 : $wallet['kk_balance'];
        //提现中
        $nowWithdrawPrice = $this->model_finance_withdraw_order->getAllPrice($userId);
        $data['nowWithdrawPrice'] = (int) $nowWithdrawPrice;
        //提现比例
//        $config = config('pay');
//        $data['kk_to_withdraw'] = (string) $config['wallet_change_ratio']['kk_to_withdraw'];
        $config=$this->systemConfigModel->getDefaultSystemConfig();
        $data['kk_to_withdraw'] = $config['kk_to_withdraw']['value'];
        return compact('code', 'msg', 'data');
    }

    public function bindBank($userId, $province, $openBank, $bankNo, $phone)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            try {
                //实名认证
                $user_verify = $this->userVerifyModel->getInfoByUser(['user_id'=>$userId ,'result_code'=>1000]);
                if(empty($user_verify)) {
                    $code = Hint::USER_NOT_CERTIFICATION;
                    break;
                }
                $name = $user_verify['idcard_name'];
                $cardNo = strtoupper($user_verify['idcard_number']);
                $openName = $user_verify['idcard_name'];

                //自由职业者签约
                $body['uuid'] = $userId;
                $body['list'] = [
                    [
                        'name' => $name,
                        'cardNo' => $cardNo,
                        'province' => $province,
                        'openBank' => $openBank,
                        'openName' => $openName,
                        'bankNo' => $bankNo,
                        'phone' => $phone,
                    ]
                ];
                $blsRes = $this->blsLib->workerRegister($body);
                if(empty($blsRes)) {
                    $code = Hint::BANK_CREATE_FAIL;
                    break;
                }
                if($blsRes['code']!=10) {
                    $code = Hint::BANK_CREATE_FAIL;
                    $msg = '银行卡创建失败:'.$blsRes['msg'];
                    break;
                }
                if(empty($blsRes['batchNo'])) {
                    $blsRes['batchNo'] = '';
                    // $code = Hint::BANK_CREATE_FAIL;
                    // break;
                }

                //数据记录
                $data['bank_id'] = $this->financeBankModel->bindBankBls($userId, $name, $cardNo, $province, $openBank, $openName, $bankNo, $phone, $blsRes['batchNo']);
                if ($data['bank_id'] <= 0) {
                    $code = Hint::BANK_CREATE_FAIL;
                    break;
                }

            } catch (\Exception $e) {
                $code = Hint::NOT_ALLOWED;
                $msg = $e->getMessage();
                break;
            }
        } while (false);

        return compact('code', 'msg', 'data');
    }

    public function bankList($userId)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = ['list'=>[]];

        $list = $this->financeBankModel->bankList($userId);
        foreach($list as $val) {
            $data['list'][] = [
                'bankId' => $val['id'],
                'name' => $val['user_name'],
                'cardNo' => $val['idcard_no'],
                'province' => $val['province'],
                'openBank' => $val['bank_address'],
                'openName' => $val['owner_name'],
                'bankNo' => $val['bank_no'],
                'phone' => $val['mobile_phone'],
                'created_at' => $val['created_at'],
            ];
        }

        return compact('code', 'msg', 'data');
    }

    public function bankDelete($userId, $bankId)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        $this->financeBankModel->bankDelete($userId, $bankId);

        return compact('code', 'msg', 'data');
    }

    public function clearUserbankAll($userId)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        $this->financeBankModel->bankDelete($userId, 0);

        return compact('code', 'msg', 'data');
    }



}
